var EBEventList = Class.create( );
EBEventList.prototype = {

	initialize: function( ) {
		this.container = $($RGC().eventListContainer);
		$RGE('event-list-data').addObserver(this);
	},
	
	start: function() {
		$RGE('event-list-data').load();
	},
	
	update: function() {
		if(!$RGE('event-list-data').isLoaded( )) return;
		$A($RGE('event-list-data').events).each(
			function(el) {
				this.insertEvent(el);
			}.bind(this)
		);
		$RGE('event-list-data').removeObserver();
	},
	
	setEvent: function(event) {
		//En primer lugar observamos si se encuentra en el DOM o no
		var e = Event.element(event);
		if(e.tagName == 'SPAN')	e = e.parentNode;
		var dateNode = Element.down($(e), '.date');
		if(dateNode != null) $RGE('browser').doPositioningByEvent(e.getAttribute('title'), dateNode.getAttribute('title'));
	},
	
	insertEvent: function(e) {
      var event= document.createElement('div');
      event.setAttribute("class","event-container " + e['keywords']);
      event.setAttribute("id","l-event-"+e['id']);
      event.setAttribute("title", "event-"+e['id']);
      
	  var dateParts = e['date'].split("/");
	  var hourParts = e['hour'].split(':');
	  var eDate = new EBDateTool(new Date(dateParts[2], parseInt(dateParts[1])-1, dateParts[0], hourParts[0], hourParts[1]));
	  var html = '<span class="title priority-' + e['priority'] + '">' + e['title'] + '</span>';
	  html += '<span class="date" title="' + eDate.date.toString() + '">' + eDate.getEventString() + '</span>';
	  if(e['keywords'] != '') html += '<span class="keywords"><em>Keywords:</em> ' + e['keywords'] + '</span>';
	  
	  event.innerHTML = html;
      //event.appendChild(document.createTextNode());
      this.container.appendChild(event);
      Event.observe(event, 'click', this.setEvent.bindAsEventListener(this));
	}

}
